package cmd

import (
	"strings"
)

type DataCleaner interface {
	Clean(string) string
}

type DataCleanFunc func(string) string

func (f DataCleanFunc) Clean(s string) string {
	return f(s)
}

var (
	emptyReplacer                    = strings.NewReplacer(" ", "")
	collapseReviewButtonTextReplacer = strings.NewReplacer("收起评价一", "", "收起评价", "~")
	threeDotReplacer                 = strings.NewReplacer("…", "")
)

func RemoveEmtpy(s string) string {
	return emptyReplacer.Replace(s)
}

func RemoveCollapseReviewButtonText(s string) string {
	return collapseReviewButtonTextReplacer.Replace(s)
}

func CutoffShopNameTextTail(s string) string {
	idx := strings.Index(s, "~手机扫码")
	if idx < 0 {
		return s
	}
	return s[:idx]
}

func RemoveThreeDot(s string) string {
	return threeDotReplacer.Replace(s)
}

func DataClean(s string, cleaners ...DataCleaner) string {
	ret := s
	for _, cleaner := range cleaners {
		ret = cleaner.Clean(ret)
	}
	return ret
}

// var wg sync.WaitGroup
// for i := range nodes {
// 	wg.Add(1)

// 	go func(idx int) {
// 		defer wg.Done()
// 		var buf []byte
// 		if err := chromedp.Run(ctx,
// 			chromedp.Screenshot(fmt.Sprintf("#shop-tabs > div.shop-tab-recommend.J-panel > ul > li:nth-child(%d) > p", idx), &buf, chromedp.NodeVisible, chromedp.ByID),
// 			chromedp.ActionFunc(func(ctx context.Context) error {
// 				slog.Infof("idx=>%d", idx, buf)
// 				return tr._dumpPicBytesToFile(buf, fmt.Sprintf(`tmp/shop_%s/recommand-dish-%d.png`, shopID, idx))
// 			}),
// 		); err != nil {
// 			errOut = err
// 		}
// 	}(i + 1)
// }
// wg.Wait()
